package coreservlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LongServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/html");
		// Change the definition of "out" depending on whether
		// or not gzip is supported.
		PrintWriter out;
		if (GzipUtilities.isGzipSupported(req)
				&& !GzipUtilities.isGzipDisabled(req)) {
			out = GzipUtilities.getGzipWriter(resp);
			resp.setHeader("Content-Encoding", "gzip");
		} else {
			out = resp.getWriter();
		}
		// Once "out" has been assigned appropriately, the
		// rest of the page has no dependencies on the type
		// of writer being used.
		String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "
				+ "Transitional//EN\">\n";
		String title = "Long Page";
		out.println(docType + "<HTML>\n" + "<HEAD><TITLE>" + title
				+ "</TITLE></HEAD>\n" + "<BODY BGCOLOR=\"#FDF5E6\">\n"
				+ "<H1 ALIGN=\"CENTER\">" + title + "</H1>\n");
		String line = "Blah, blah, blah, blah, blah. "
				+ "Yadda, yadda, yadda, yadda.";
		for (int i = 0; i < 10000; i++) {
			out.println(line);
		}
		out.println("</BODY></HTML>");
		out.close(); // Needed for gzip; optional otherwise.
	}

}
